Unity3D Building Damage Tutorial- Part V b - Thick Mesh in diffrent way
Well, I was wandering how to implement damage in thick mesh, and i realized that making mesh thick can be implemented much easier. In previous part I manually edited the Aliases List, Triangle LIst, VTAA, and then mesh.triangles and mesh.verticles. But really, we can just edit the last two, and than re-construct our mesh using methods in constructor. Some parts of previous tutorials will be used, some not. To have "thick" meshes we will have to: #Add "twin" verticles #Add "twin" triangles #Update lists and VerticleToAliasArray (VTAA) Thats all. Twins When i was writing this tutorial, I named some certain things "twins". My idea is that we will make, internal, inverted mesh. It will have the same amount of triangles and vertices. The "twin" verticle will be a verticle "made of" original verticle and than moved. Its number will be number of original + offset. Similarly "twin" triangles and Aliases are made. Offseting So, when i was thinking about this, i realised of something. Lets say, that we have a simple cube, like than in that picture. It has 8 Aliases. Lets say of numbers 1-8. 'In fact, they would have numbers 0-7, but i have made a mistake. ' In addition 12 triangles and 36 verticles. Than, we add inside "twin cube". An offset of Aliases would be its number in pervious cube, in this case 8. An twin of Alias number 2 would be 2 + 8 = 10. In that way we can easily calculate numbers of every twin. A twin of triangle number 10 would be 10+12 = 22. etc. MakeMeshThick() Method Well, the starting method. It will be called lets say in StartFire, so when player clicks on the building. public void MakeMeshThick(){ AddTwinsToVerticleArray(); AddTwinsToTriangleAlias(); Initialise(); ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight(); for(int i=Aliases.Count/2; i Vector3[] OldVerticleList = mesh.vertices; Vector3[] NewVerticleList = new Vector3OldVerticleList.Length; *here we just make two arrays, of the same lenght. OldVerticleList will just store the original position of vertices float Wall_Thickness = 0.005f; Vector3 TargetPosition; Vector3 NewPosition ; *float Wall_Thickness = 0.005f; Hard coded distance between an Alias and its twin. Later, I'll change it, and will not be hard coded of course. *Vector3 TargetPosition; Vector3 NewPosition ; Two Vector3. Will be used later. *int offset = Aliases.Count; Here I just count the offset. *TargetPosition = mesh.verticesi - mesh.normalsi; This is intresting. But before I am going to explain this, simple question. How we are going to know, which way we should move the Alias, for it to be "in" the mesh. First, i thought about scaling, but that was working well only with simple meshes with its "center" position in absolute inside. Than, I found out about vertex normals. More specific data can be found on wiki. What is intresting for us, is that in mesh.normals there are some specific vector3 for every variable. This are vectors pointing outside from the mesh, like in the picture. Using then, we can easily count the twin position. NewPosition = Vector3.MoveTowards(mesh.verticesi, TargetPosition, Wall_Thickness); NewVerticleListi = NewPosition; Two lines, two intresting things. First, i count TargetPosition, by decreasing the relativePosition of verticle by the normal value. (Decreasing!. We want a point inside the mesh, not outside.) Vector3[] final = new Vector3+ NewVerticleList.Length; Vector3[] final = new Vector3+ NewVerticleList.Length; OldVerticleList.CopyTo(final, 0); NewVerticleList.CopyTo(final, OldVerticleList.Length); mesh.vertices = final; Merging two arrays and setting the result as vertices. AddTwinsToTriangleArray method private void AddTwinsToTriangleAlias(){ int[] OldTriangleList = mesh.triangles; int[] NewTriangleList = new int OldTriangleList.Length; int offset = (mesh.vertices.Length/2); for(int i=0; iint[] final = new int+ NewTriangleList.Length; int[] final = new int+ NewTriangleList.Length; OldTriangleList.CopyTo(final, 0); NewTriangleList.CopyTo(final, OldTriangleList.Length); mesh.triangles = final; Merging two arrays and updating mesh.triangles. Back to MakeMeshThick method Now, we have edited mesh. Our custom list have to be updated. Initialise(); ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight(); Starting the methods from constructor. This Initialise and CalculateMinimumHeight are not really necessary, but i put them here. for(int i=Aliases.Count/2; iEnding Now, if we look into a mesh there will be untextured area, as uv has not been yet changed. For files, go to Github . Category:Unity3D Building Damage Tutorial Category:Unity3D Category:C Sharp